package longrise.phone.com.bjjt_henan.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;

/**
 * 我的机动车列表的适配器
 */
public class MyCarAdapter extends BaseAdapter {

    private Context mContext;

    private EntityBean[] lists;

    private View.OnClickListener listener;

    public MyCarAdapter(Context context, View.OnClickListener listener) {
        this.mContext = context;
        this.listener = listener;
    }

    public void setData(EntityBean[] beans) {
        this.lists = beans;
    }

    @Override
    public int getCount() {
        return (lists == null) ? 0 : lists.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_mycar_item, null);
            holder.iconIV = (ImageView) convertView.findViewById(R.id.mycar_item_icon);
            holder.nameTV = (TextView) convertView.findViewById(R.id.mycar_item_name);
            holder.carTypeTV = (TextView) convertView.findViewById(R.id.mycar_item_identityno);
            holder.frameTV = (TextView) convertView.findViewById(R.id.mycar_item_fileno);
            holder.modifyLayout = (LinearLayout) convertView.findViewById(R.id.mycar_item_modify);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        EntityBean bean = lists[position];
        //行车证图片
        String pic = bean.getString("drivingnoimg");
        if (!TextUtils.isEmpty(pic)) {
            ImageLoader.getInstance().displayImage(pic, holder.iconIV, CommontUtils.getOptions());
        }
        //车牌
        holder.nameTV.setText(bean.getString("carno"));
        //车型
        String type = bean.getString("cartype");
        String typeinfo = (TextUtils.equals(type, "2")) ? "大型汽车" : "小型汽车";
        holder.carTypeTV.setText("车型：" + typeinfo);
        //车架号
        holder.frameTV.setText("车架号：" + bean.getString("identificationnum"));
        //点击修改
        holder.modifyLayout.setTag(bean);
        holder.modifyLayout.setOnClickListener(listener);

        return convertView;
    }

    private class ViewHolder {
        private ImageView iconIV;//驾驶证
        private TextView nameTV;//用户名
        private TextView carTypeTV;//车型
        private TextView frameTV;//车架号
        private LinearLayout modifyLayout;
    }
}
